package com.zzyl.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.zzyl.base.PageBean;
import com.zzyl.dto.NursingProjectDto;
import com.zzyl.dto.NursingProjectPageQueryDto;
import com.zzyl.entity.NursingProject;
import com.zzyl.entity.NursingProjectPlan;
import com.zzyl.enums.BasicEnum;
import com.zzyl.exception.BaseException;
import com.zzyl.mapper.NursingPlanMapper;
import com.zzyl.mapper.NursingProjectMapper;
import com.zzyl.mapper.NursingProjectPlanMapper;
import com.zzyl.service.NursingProjectService;
import com.zzyl.vo.NursingProjectVo;
import lombok.RequiredArgsConstructor;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

@Service
@RequiredArgsConstructor
public class NursingProjectServiceImpl implements NursingProjectService {


    final private NursingProjectMapper nursingProjectMapper;
    final private NursingProjectPlanMapper nursingProjectPlanMapper;

    /**
     * 分页条件查询护理项目
     *
     * @param dto
     * @return
     */
    @Override

    @Cacheable(cacheNames = "zzyl:nursing:project", key = "#root.methodName+':'+#dto.hashCode()")
    public PageBean<NursingProjectVo> pageQuery(NursingProjectPageQueryDto dto) {
        //开启分页查询
        PageHelper.startPage(dto.getPageNum(), dto.getPageSize());
        //条件查询
        Page<NursingProjectVo> page = nursingProjectMapper.pageQuery(dto);
        //通过PageBean的of方法快速构建一个对象
        return PageBean.of(page, NursingProjectVo.class);//
    }

    /**
     * 新增护理项目
     *
     * @param nursingProjectDTO 护理项目数据传输对象
     */
    @Override
    @CacheEvict(cacheNames = "zzyl:nursing:project", allEntries = true)

    public void add(NursingProjectDto nursingProjectDTO) {
      /*  NursingProject nursingProject = new NursingProject();
        BeanUtils.copyProperties(nursingProjectDTO, nursingProject);*/
        //将DTO转换为实体类
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDTO, NursingProject.class);
        //设置创建时间、更新时间、创建人、更新人
        nursingProject.setCreateTime(LocalDateTime.now());
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setCreateBy(1351531513L);
        nursingProject.setUpdateBy(1351531513L);
        //插入数据
        nursingProjectMapper.insert(nursingProject);
    }

    /**
     * 根据ID查询护理项目
     *
     * @param id
     * @return
     */

    @Override
    @Cacheable(cacheNames = "zzyl:nursing:project", key = "#root.methodName +':'+#id")
    public NursingProjectVo getById(Long id) {
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        return BeanUtil.toBean(nursingProject, NursingProjectVo.class);
    }


    /**
     * 更新护理项目信息
     *
     * @param nursingProjectDto 护理项目数据传输对象
     */
    @Override
    @CacheEvict(cacheNames = "zzyl:nursing:project", allEntries = true)
    public void update(NursingProjectDto nursingProjectDto) {
        NursingProject nursingProject = BeanUtil.toBean(nursingProjectDto, NursingProject.class);
        nursingProject.setUpdateTime(LocalDateTime.now());
        nursingProject.setUpdateBy(1351531513L);
        nursingProjectMapper.update(nursingProject);
    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nursing:project", allEntries = true)
    public void enableOrDisable(Long id, Integer status) {
        //mapper中的方法，只能叫select、insert、update、delete
        //体会三层结构特点：mapper和业务没有关系，mapper只是一个数据访问对象

        //通过builder构建对象
        NursingProject nursingProject = NursingProject.builder().id(id).status(status).build();
        nursingProjectMapper.update(nursingProject);

    }

    @Override
    @CacheEvict(cacheNames = "zzyl:nursing:project", allEntries = true)
    public void delete(Long id) {
        //判断该护理项目是否被启用，若启用，则抛异常
        NursingProject nursingProject = nursingProjectMapper.getById(id);
        if (nursingProject.getStatus() == 1) {
            throw new BaseException(BasicEnum.ENABLED_CANNOT_DELETED);
        }
        //判断该护理项目是否关联护理计划，若关联，则抛异常
        List<NursingProjectPlan>  list = nursingProjectPlanMapper.getByProjectId(id);
        if(CollUtil.isNotEmpty(list)) {
            throw new BaseException(BasicEnum.DELETE_PROJECT_FAIL);
        }

        nursingProjectMapper.delete(id);
    }

    //查询所有
    @Override
    @Cacheable(cacheNames = "zzyl:nursing:project", key = "#root.methodName")
    public List<NursingProject> findAll() {
       return nursingProjectMapper.findAll();

    }
}